import { Component, OnInit } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { ConfirmationService, MessageService } from 'primeng/api';
import { AddrService } from 'src/app/service/addr.service';
import { getCity } from 'src/app/store/utils/utils.reducer';

@Component({
    selector: 'app-address-district',
    templateUrl: './address-district.component.html',
    styleUrls: ['./address-district.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class AddressDistrictComponent implements OnInit {
    cityList: any;
    editTag = true;
    display = false;
    currentItem: any;
    addForm = {
        name: '',
        zoneId: '',
    };
    blankForm = {
        name: '',
        zoneId: '',
    };
    districtList: any;
    constructor(
        private confirmationService: ConfirmationService,
        private messageService: MessageService,
        private addrService: AddrService,
        private store:Store
    ) {
        this.store.pipe(select(getCity)).subscribe((data)=>{
            this.cityList = data
            console.log(data)
        })
    }

    ngOnInit() {
        this.search();
    }
    async search() {
        // await store.dispatch('utils/fetchAddr')
        const res = await this.addrService.fetchDistrictList();
        if (res) {
            this.districtList = res;
        }
        console.log(res);
    }
    submit() {
        if (this.editTag) {
            this.edit();
        } else {
            this.add();
        }
    }
    async add() {
        const r = await this.addrService.AddDistrictInfoByConfig({
            cityId: this.addForm.zoneId,
            districtName: this.addForm.name,
        });
        if (r.code === '200') {
            this.search();
            this.display = false;
            this.messageService.add({
                severity: 'success',
                summary: '成功',
                detail: '你已经添加',
            });
            return true;
        }
        return false;
    }
    async edit() {
        const r = await this.addrService.editDistrictInfoByConfig({
            districtId: this.currentItem.zoneId,
            districtName: this.addForm.name,
            cityId: this.addForm.zoneId,
        });
        if (r.code === '200') {
            this.search();
            this.display = false;
            this.messageService.add({
                severity: 'success',
                summary: '成功',
                detail: '你已经修改',
            });
            return true;
        }
        return false;
    }
    async delete(item) {
        this.confirmationService.confirm({
            message: '请问你确认删除吗 ?',
            header: '警告',
            icon: 'pi pi-exclamation-triangle',
            accept: async () => {
                const res = await this.addrService.deleteDistrictInfoById({
                    id: item.zoneId,
                });
                if (res.code == 200) {
                    this.messageService.add({
                        severity: 'info',
                        summary: '确认',
                        detail: '你已经删除',
                    });
                    this.search();
                }
            },
        });
    }
    openDialog(a, item?) {
        this.display = true;
        this.editTag = a;
        this.currentItem = item ? item : null;
        if (a) {
            this.addForm.zoneId = item.pid;
            this.addForm.name = item.district
        } else {
            this.addForm = { ...this.blankForm };
        }
    }
}
